﻿#include "UserInfo.h"
#include "../util/JsonUtil.h"
#include "../util/Md5Util.h"

UserInfo::UserInfo()
{
}

UserInfo::~UserInfo()
{
}

std::string UserInfo::GetUsername()
{
	return m_username;
}

void UserInfo::SetUsername(std::string id)
{
	m_username = id;
}

std::string UserInfo::GetPassword()
{
	return m_password;
}

void UserInfo::SetPassword(std::string device)
{
	m_password = device;
}

void UserInfo::FromJson(std::string json)
{
	if (json.empty())
	{
		return;
	}
	// {"username": "admin", "password": "dd123456"}
	JsonUtil jsonUtil;
	jsonUtil.Parse(json);
	m_username = jsonUtil.GetString("username", "admin");
	m_password = jsonUtil.GetString("password", "dd123456");
}

std::string UserInfo::ToJson()
{
	std::stringstream stream;
	try
	{
		boost::property_tree::ptree pt;
		pt.put("username", m_username);
		pt.put("password", m_password);
		boost::property_tree::write_json(stream, pt);
	}
	catch (boost::property_tree::ptree_error pt)
	{
		pt.what();
		return "";
	}
	return stream.str();
}

std::string UserInfo::GetToken()
{
	return Md5Util::GetTextMd5(m_username + ":" + m_password);
}